<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Hash;

class UserController extends Controller
{
    /**
     * 用户的添加
     */
    public function getAdd()
    {
    	return view('admin.user.add');
    }
    /**
     * 用户的插入
     */
    public function postInsert(Request $request)
    {
    	//进行表单验证
    	$this->validate($request,[
    			//用户名=>必填|格式|名字唯一
    			'username' => 'required|regex:/^\w{8,18}$/|unique:user,username',
    			//密码=>必填|格式|两次密码是否一直
    			'password' => 'required|regex:/^\S{6,20}$/|same:repassword',
    			//邮箱=>必填|格式
    			'email' => 'required|regex:/(\w*)@(\w*).com/'
    		],[
    			'username.required' => '用户名不能为空',
    			'username.regex' => '用户名格式不正确',
    			'username.unique' => '用户名已存在',
    			'password.regex' => '密码格式不正确',
    			'password.same' => '两次密码不一致',
    			'email.required' => '邮箱不能为空',
    			'email.regex' => '邮箱格式不正确'
    		]);
    	//获取数据
    	$data = $request->except(['repassword','_token']);
    	//加密
    	$data['password'] = Hash::make($data['password']);
    	//处理头像
    	if($request->hasFile('profile')){
    		$fileName = date('Ymd',time()).rand(100000,999999);
    		$suffix = $request->file('profile')->getClientOriginalExtension();//获取文件后缀名
    		$fileName = $fileName.'.'.$suffix;
    		$request->file('profile')->move('./Uploads/',$fileName);
    		$data['profile'] = '/Uploads/'.$fileName;
    	}
        $data['register'] = date('Ymd',time());
        // dd($data);
    	//插入数据
    	$res = DB::table('user')->insert($data);

    	// 检测
    	if($res){
    		return redirect('/user/index')->with('info','添加成功 ');
    	}else{
    		return back()->with('error','添加失败');
    	}
    }

    /**
     * 列表显示功能
     */
    public function getIndex(Request $request)
    {
    	//读取数据库
    	$users = DB::table('user')->where(function($query) use($request){
    			$k = $request->input('keyword');
    			if(!empty($k)){
    				$query->where('username','like','%'.$k.'%');
    			}
    	})->paginate($request->input('num',10));
    	// dd($users);
    	return view('admin.user.index',['user'=>$users,'request'=>$request]);
    }

     /**
      * 用户的修改页面
      */
    public function getEdit(Request $request)
    {
    	
        //读取当前id的用户信息
    	$id = $request->input('id');
    	//读取数据库
    	$user = DB::table('user')->where('id',$id)->first();
        $path = $request->path();
        $path .= '?id='.$id;
        session(['redirect'=>$path]);
    	//解析模板
    	return  view('admin.user.edit',['user'=>$user]);
    }

     /**
     * 用户的更新操作
     */
    public function postUpdate(Request $request)
    {
        // dd(session('redirect'));
        //表单验证
    	$this->validate($request,[
    			//用户名=>必填|格式|名字唯一
    			'username' => 'required|regex:/^\w{8,18}$/|unique:user,username',
    			//邮箱=>必填|格式
    			'email' => 'required|regex:/(\w*)@(\w*).com/'
    		],[
    			'username.required' => '用户名不能为空',
    			'username.regex' => '用户名格式不正确',
    			'username.unique' => '用户名已存在',
    			'email.required' => '邮箱不能为空',
    			'email.regex' => '邮箱格式不正确'
    		]);
    	//图片上传操作
    	$data = $request->except(['_token','id']);
        // dd($data);
    	//处理上传的文件
    	$path = $this->upload($request);
    	if(!empty($path)){
    		$data['profile'] = $path;
    	}
    	//更新
    	$res = DB::table('user')->where('id',$request->input('id'))->update($data);
    	// dd($res);
    	if($res){
            if(session('redirect')){
                return redirect(session('redirect'))->with('info','修改成功');
            }
    		return redirect('/user/index')->with('info','修改成功');
    	}else{
    		return back()->with('error','修改失败');
    	}
    }

    /**
     * 删除操作
     */
    public function getDelete(Request $request)
    {
    	$id = $request->input('id');
    	//执行删除
    	$res = DB::table('user')->where('id',$id)-> delete();
    	if($res){
    		return back()->with('info','删除成功');
    	}else{
    		return back()->with('error','删除失败');
    	}
    }

    /**
     * 封装上传操作代码
     */
    public function upload($request)
    {
    	if($request->hasFile('profile')){
    		$fileName = date('Ymd',time()).rand(100000,999999);
    		$suffix = $request->file('profile')->getClientOriginalExtension();//获取文件后缀名
    		$fileName = $fileName.'.'.$suffix;
    		$request->file('profile')->move('./Uploads/',$fileName);
    		$data['profile'] = '/Uploads/'.$fileName;
    		return $data['profile'];
    	}
    }

    // 个人中心
    public function getCenter(Request $request)
    {
        $id = $request->input('id');
        // 获取用户信息
        $user = DB::table('user')->where('id',$id)->first();
        // 获取用户的回复信息
        $replys = DB::table('reply')
            ->join('card','reply.tid','=','card.id')
            ->select('reply.*','card.title') 
            ->where('u_id',$id)->get();
            // dd($replys);
        if(!empty($replys)){
            // $rcount = count($replys);
            $tid = $replys[0]->tid;
            $card = DB::table('card')->where('id',$tid)->first();
            $title = $card->title;
        }else{
            $title = '';
        }

        // 获取用户的发帖信息
        $cards = DB::table('card')->where('uid',$id)->get();
        foreach($cards as $k=>$v){
            $v->count = DB::table('reply')->where('tid',$v->id)->count();
        }

        // dd($cards);
        return  view('home.user.Ucenter',['user'=>$user,'replys'=>$replys,'id'=>$id,'title'=>$title,'cards'=>$cards]);
    }

    // 前台个人信息编辑
    public function getCenteredit(Request $request)
    {
        // $id = $request->input('id');
        $id = session('id');
        $user = DB::table('user')->where('id',$id)->first();
        // dd($id);
        session(['redirect'=>$request->path()]);
        return  view('home.user.edit',['user'=>$user]);
    }


    // 前台个人信息确认编辑
    public function postCenterupdate(Request $request)
    {
        $data = $request->all();
        // $data = $request->except(['_token','id']);
        dd($data);

    }

}
